﻿namespace AnyOffice.Components
{
    using System;
    using System.Globalization;
    using System.Threading;

    public sealed class VBString
    {
        internal static readonly CompareInfo m_InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo;

        public static Array CopyArray(Array arySrc, Array aryDest)
        {
            if (arySrc != null)
            {
                int length = arySrc.Length;
                if (length == 0) return aryDest;
                if (aryDest.Rank != arySrc.Rank) throw new InvalidCastException("");
                int num2 = aryDest.Rank - 2;
                for (int i = 0; i <= num2; i++)
                {
                    if (aryDest.GetUpperBound(i) != arySrc.GetUpperBound(i)) throw new ArrayTypeMismatchException("");
                }
                if (length > aryDest.Length) length = aryDest.Length;
                if (arySrc.Rank > 1)
                {
                    int rank = arySrc.Rank;
                    int num5 = arySrc.GetLength(rank - 1);
                    int num6 = aryDest.GetLength(rank - 1);
                    if (num6 != 0)
                    {
                        int num7 = Math.Min(num5, num6);
                        int num8 = arySrc.Length / num5 - 1;
                        for (int j = 0; j <= num8; j++)
                        {
                            Array.Copy(arySrc, j * num5, aryDest, j * num6, num7);
                        }
                    }
                    return aryDest;
                }
                Array.Copy(arySrc, aryDest, length);
            }
            return aryDest;
        }

        private static bool IsArrayEmpty(Array ary)
        {
            return (ary == null || ary.Length == 0);
        }

        public static string Join(string[] SourceArray, string Delimiter)
        {
            string str;
            try
            {
                if (IsArrayEmpty(SourceArray)) return null;
                if (SourceArray.Rank != 1) throw new ArgumentException("Rank not 1");
                str = string.Join(Delimiter, SourceArray);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string Replace(string Expression, string Find, string Replacement)
        {
            string str;
            int num = 1;
            int length = -1;
            StringCompareMethod text = StringCompareMethod.Text;
            try
            {
                if (length < -1) throw new ArgumentException("Count 0", "Replace");
                if (num <= 0) throw new ArgumentException("Start 0", "Replace");
                if (Expression == null || num > Expression.Length) return null;
                if (num != 1) Expression = Expression.Substring(num - 1);
                if (Find == null || Find.Length == 0 || length == 0) return Expression;
                if (length == -1) length = Expression.Length;
                str = Join(Split(Expression, Find, length + 1, text), Replacement);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str;
        }

        public static string[] Split(string Expression, string Delimiter, int Limit, StringCompareMethod Compare)
        {
            string[] strArray;
            try
            {
                int length;
                if (Expression == null || Expression.Length == 0) return new string[] { "" };
                if (Limit == -1) Limit = Expression.Length + 1;
                if (Delimiter == null)
                    length = 0;
                else
                    length = Delimiter.Length;
                if (length == 0) return new string[] { Expression };
                strArray = SplitHelper(Expression, Delimiter, Limit, (int) Compare);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return strArray;
        }

        private static string[] SplitHelper(string sSrc, string sFind, int cMaxSubStrings, int Compare)
        {
            CompareInfo invariantCompareInfo;
            CompareOptions ordinal;
            int length;
            int index = 0;
            int startIndex = 0;
            int num4 = 0;
            if (sFind == null)
                length = 0;
            else
                length = sFind.Length;
            if (sSrc == null)
                num4 = 0;
            else
                num4 = sSrc.Length;
            if (length == 0) return new string[] { sSrc };
            if (num4 == 0) return new string[] { sSrc };
            int num5 = 20;
            if (num5 > cMaxSubStrings) num5 = cMaxSubStrings;
            string[] arySrc = new string[num5 + 1];
            if (Compare == 0)
            {
                ordinal = CompareOptions.Ordinal;
                invariantCompareInfo = m_InvariantCompareInfo;
            }
            else
            {
                invariantCompareInfo = Thread.CurrentThread.CurrentCulture.CompareInfo;
                ordinal = CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreCase;
            }
            while (startIndex < num4)
            {
                string str;
                int num6 = invariantCompareInfo.IndexOf(sSrc, sFind, startIndex, num4 - startIndex, ordinal);
                if (num6 == -1 || index + 1 == cMaxSubStrings)
                {
                    str = sSrc.Substring(startIndex);
                    if (str == null) str = "";
                    arySrc[index] = str;
                    break;
                }
                str = sSrc.Substring(startIndex, num6 - startIndex);
                if (str == null) str = "";
                arySrc[index] = str;
                startIndex = num6 + length;
                index++;
                if (index > num5)
                {
                    num5 += 20;
                    if (num5 > cMaxSubStrings) num5 = cMaxSubStrings + 1;
                    arySrc = (string[]) CopyArray(arySrc, new string[num5 + 1]);
                }
                arySrc[index] = "";
                if (index == cMaxSubStrings)
                {
                    str = sSrc.Substring(startIndex);
                    if (str == null) str = "";
                    arySrc[index] = str;
                    break;
                }
            }
            if (index + 1 == arySrc.Length) return arySrc;
            return (string[]) CopyArray(arySrc, new string[index + 1]);
        }
    }
}

